$(document).ready(function() {

	$("#checkReadkey").hide();
	$("#checkWritekey").hide();
	$("#ok").hide();
	$("#loading").hide();


	//for main page
	$("#check").hide();
	//$("#sub").hide();
	var key = document.getElementById("key");
	
	$(key).bind('blur', function()
	{
        if (this.value == "")
		{
			this.value = "Enter your key..."
			$("#check").hide();
			//$("#sub").hide();
			return;
		}
	    /*if(this.value.length !=5)
	    {
	    	$("#check").show();
			$("#sub").hide();
			return;
        }

	    if(this.value.length == 5)
		$("#sub").show();
		$("#check").hide();*/
	})
		.bind('focus', function()
		{
			if (this.value=="Enter your key...")
         		this.value = "";
        });



	var readkey = document.getElementById("readkey");
	var writekey = document.getElementById("writekey");
	var info = document.getElementById("information");
	var load = document.getElementById("loading");

	$(load).hide();
	$("#ok").show();
	
	$(readkey).bind('blur', function()
	{
		if (this.value == "")
	    {
			this.value = "Enter your readkey...";
			$("#checkReadkey").hide();
			//$("#ok").hide();
			return;
	    }

	    /*if (this.value.length !=5 || this.value == writekey.value)
	    {
			$("#checkReadkey").show();
			$("#ok").hide();
			return;
	    }

	    if(this.value.length ==5)
			$("#checkReadkey").hide();

	    if(writekey.value.length ==5)
	    {
	    	$("#checkReadkey").hide();
			$("#checkWritekey").hide();
			$("#ok").show();
   	    }*/

    }).bind('focus', function()
		{
			if (this.value=="Enter your readkey...")
				this.value = "";
        });

        $(writekey).bind('blur', function()
		{
            if (this.value == "")
			{
                this.value = "Enter your writekey...";
				$("#checkWritekey").hide();
				//$("#ok").hide();
				return;
			}

			/*if (this.value.length !=5 || this.value == readkey.value)
			{
				$("#checkWritekey").show();
				$("#ok").hide();
				return;
			}

			if (this.value.length ==5)
				$("#checkWritekey").hide();

			if (readkey.value.length ==5)
			{
				$("#checkWritekey").hide();
				$("#checkReadkey").hide();
				$("#ok").show();
			}*/
        })
        	.bind('focus', function()
			{
        		if (this.value=="Enter your writekey...")
         			this.value = "";
            });

	$(info).bind('blur', function()
	{
		if (this.value == "")
			this.value = "Your information...";
    })
		.bind('focus', function()
		{
			if (this.value=="Your information...")
				this.value = "";
        });

});

function scramblingdata()
{
	
	//var read=addnewdata.readkey.value;
	//var write=addnewdata.writekey.value;
	//$(readkey).hide();
	//$(writekey).hide();
	//$(info).hide();
	//$("#ok").hide();
	//$(load).show();
	//Здесь должна работать крутилка
	//there will hash 100%
	form = document.forms.addnewdata;
	
	//var key = addnewdata.readkey.value;
	//var texttoencrypt = addnewdata.information.value;
	
	var readkey = document.getElementById("readkey"); //$("#readkey").val();
	var writekey = document.getElementById("writekey");
	var texttoencrypt = document.getElementById("information"); //$("#information").val();
	
	var password = readkey.value.toString();
	var text = texttoencrypt.value.toString();
	
	$("#readkey").hide();
	$("#writekey").hide();
	$("#information").hide();
	$("#ok").hide();
	$("#label1").hide();
	$("#label2").hide();
	$("#loading").show();
	
	texttoencrypt.value = sjcl.encrypt(password, text);
	
	readkey.value = hex_md5(readkey.value);
	writekey.value = hex_md5(writekey.value);

	form.submit();
}


function scramblingkey()
{
	form = document.forms.getdata;
	
	$("#key").hide();
	$("#sub").hide();
	$("#check").hide();
	$("#label").hide();
	$("#loading").show();
	
	var key = document.getElementById("key");
	key.value = hex_md5(key.value);
	decrypt();
	form.submit();	
}

function decryptdata()
{
	var key = document.getElementById("key");
	var s = document.getElementById("information");
	
	var pass = key.value.toString();
	var text = s.value.toString();
	
	s.value = sjcl.decrypt(pass, text)
}

function decryptall()
{
	var targets = document.getElementsByName("toDecrypt");
	var key = document.getElementById("key");
	var pass = key.value.toString();
	var i = 0;
		
	for (i = 0; i <= targets.length; i++)
	{
		var text = targets[i].value.toString();
		targets[i].value = sjcl.decrypt(pass, text)
	}
}
